package jangdolbaeng2.admin.dao;

import jangdolbaeng2.db.MybatisConfig;
import jangdolbaeng2.vo.DeliveryVO;
import jangdolbaeng2.vo.ProductVO;
import jangdolbaeng2.vo.Product_orderVO;

import java.util.HashMap;
import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.opensymphony.xwork2.ActionSupport;

public class OrderDao extends ActionSupport {

	//주문 리스트 가져오기
	public List<Product_orderVO> orderList(int orderDay) {
		
		List<Product_orderVO> orderList = null;
		
		SqlSession sqlSession = MybatisConfig.getSqlSessionFactory().openSession();
				
		try {
			orderList = sqlSession.selectList("order.orderList", orderDay);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (sqlSession != null) sqlSession.close();
		}
		
		System.out.println("OrderDao.orderList()> orderList: " + orderList);		
		return orderList;
	}//deliveryList()

	public List<HashMap<String, Object>> orderDetail(int order_seq) {
		
		List<HashMap<String, Object>> order_detail = null;
		
		SqlSession sqlSession = MybatisConfig.getSqlSessionFactory().openSession();
				
		try {
			order_detail = sqlSession.selectList("order.orderDetail", order_seq);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (sqlSession != null) sqlSession.close();
		}
		
		System.out.println("OrderDao.orderDetail()> order_detail: " + order_detail);		
		return order_detail;
		
	}//orderDetail()

	public HashMap<String, Object> delivery(int order_seq) {
		
		HashMap<String, Object> delivery = null;
		
		SqlSession sqlSession = MybatisConfig.getSqlSessionFactory().openSession();
				
		try {
			delivery = sqlSession.selectOne("order.delivery", order_seq);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (sqlSession != null) sqlSession.close();
		}
		
		System.out.println("OrderDao.delivery()> delivery: " + delivery);		
		return delivery;
		
	}//delivery()

	public int delivery_go(int order_seq) {
		int result = 0;
		
		SqlSession sqlSession = MybatisConfig.getSqlSessionFactory().openSession();
				
		try {
			result = sqlSession.update("order.delivery_go", order_seq);
			sqlSession.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (sqlSession != null) sqlSession.close();
		}
		
		System.out.println("OrderDao.delivery_go()> result: " + result);		
		return result;
		
	}//delivery_go()
	
	public int delivery_cancel(int order_seq) {
		int result = 0;
		
		SqlSession sqlSession = MybatisConfig.getSqlSessionFactory().openSession();
				
		try {
			result = sqlSession.update("order.delivery_cancel", order_seq);
			sqlSession.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (sqlSession != null) sqlSession.close();
		}
		
		System.out.println("OrderDao.delivery_cancel()> result: " + result);		
		
		return result;
		
	}//delivery_cancel()

	public HashMap<String, Object> productDetail(int product_seq) {
		HashMap<String, Object> product = null;
		
		SqlSession sqlSession = MybatisConfig.getSqlSessionFactory().openSession();
				
		try {
			product = sqlSession.selectOne("order.productDetail", product_seq);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (sqlSession != null) sqlSession.close();
		}
		
		System.out.println("OrderDao.productDetail()> product: " + product);		
		return product;
		
	}//productDetail()

	public int deliveryComplete(int order_seq) {
		int result = 0;
		
		SqlSession sqlSession = MybatisConfig.getSqlSessionFactory().openSession();
				
		try {
			result = sqlSession.update("order.deliveryComplete", order_seq);
			sqlSession.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (sqlSession != null) sqlSession.close();
		}
		
		System.out.println("OrderDao.deliveryComplete()> result: " + result);		
		return result;
		
	}//deliveryComplete()

	public List<Product_orderVO> orderSort(char type) {
		
		List<Product_orderVO> orderList = null;
		
		SqlSession sqlSession = MybatisConfig.getSqlSessionFactory().openSession();
				
		try {
			orderList = sqlSession.selectList("order.orderSort", type);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (sqlSession != null) sqlSession.close();
		}
		
		System.out.println("OrderDao.orderSort()> orderList: " + orderList);		
		
		return orderList;
		
	}//orderSort()

	public List<Product_orderVO> orderSortbyId(String id) {
		
		List<Product_orderVO> orderList = null;
		
		SqlSession sqlSession = MybatisConfig.getSqlSessionFactory().openSession();
				
		try {
			orderList = sqlSession.selectList("order.orderSortbyId", id);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (sqlSession != null) sqlSession.close();
		}
		
		System.out.println("OrderDao.orderSortbyId()> orderList: " + orderList);		
		
		return orderList;
	}//orderSortbyId()

	public int orderPrepared(int order_seq, int product_seq) {
		
		int result = 0;
		
		SqlSession sqlSession = MybatisConfig.getSqlSessionFactory().openSession();
				
		try {
			HashMap<String, Integer> map = new HashMap<String, Integer>();
			map.put("order_seq", order_seq);	map.put("product_seq", product_seq);
			
			result = sqlSession.update("order.orderPrepared", map);
			sqlSession.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (sqlSession != null) sqlSession.close();
		}
		
		System.out.println("OrderDao.orderPrepared()> result: " + result);		
		return result;
		
	}//orderPrepared()

	public int orderNotPrepared(int order_seq, int product_seq) {
		
		int result = 0;
		
		SqlSession sqlSession = MybatisConfig.getSqlSessionFactory().openSession();
				
		try {
			HashMap<String, Integer> map = new HashMap<String, Integer>();
			map.put("order_seq", order_seq);	map.put("product_seq", product_seq);
			
			result = sqlSession.update("order.orderNotPrepared", map);
			sqlSession.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (sqlSession != null) sqlSession.close();
		}
		
		System.out.println("OrderDao.orderNotPrepared()> result: " + result);		
		return result;		
	}//orderNotPrepared()
	
	

}//class
